package com.sinosoft.jwjw.component.service.imp.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinosoft.jwjw.base.BaseCrudMapper;
import com.sinosoft.jwjw.base.BaseCrudServiceImp;
import com.sinosoft.jwjw.component.mapper.system.SDataDictionaryCodeMapper;
import com.sinosoft.jwjw.component.mapper.system.SDataDictionaryCodeNodeMapper;
import com.sinosoft.jwjw.component.model.system.SDataDictionaryCode;
import com.sinosoft.jwjw.component.model.system.SDataDictionaryCodeNode;
import com.sinosoft.jwjw.component.service.system.SDataDictionaryCodeService;
import com.sinosoft.jwjw.utils.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SDataDictionaryCodeImp extends BaseCrudServiceImp<SDataDictionaryCode,String> implements SDataDictionaryCodeService {

    @Autowired
    private SDataDictionaryCodeMapper sDataDictionaryCodeMapper;

    @Autowired
    private SDataDictionaryCodeNodeMapper sDataDictionaryCodeNodeMapper;

    @Override
    public BaseCrudMapper<SDataDictionaryCode, String> getMapper() {
        return sDataDictionaryCodeMapper;
    }


    @Override
    public List<VoZtree> getAllTree(String codeTypePara) {

        VoZtree ztree = null;
        List<VoZtree> ztreeList = new ArrayList<VoZtree>();
        Map<String,Object>  paramsMap = new HashMap<String,Object>();
        //不存在还要什么参数
        paramsMap.put("codeType",codeTypePara);
        //当condemnType不存在 则查询代码类别表
        if(StringUtil.isEmpty(codeTypePara)){
            List<SDataDictionaryCode> list =  sDataDictionaryCodeMapper.queryAll(paramsMap);
            if(!list.isEmpty()){
                for (SDataDictionaryCode codeType : list){
                    ztree = new VoZtree();
                    ztree.setId(codeType.getId());
                    ztree.setpId(codeType.getParentId());
                    ztree.setName(codeType.getCodeTypeName());
                    ztree.setCodeType(codeType.getCodeType());
                    //代码属性为代码类别  这个忘记是什么意思了
                  //  ztree.setCodeProperty("0");
                    ztreeList.add(ztree);
                }
            }
        }
        //无论codeType存不存在  都查询代码表
        List<SDataDictionaryCodeNode> codeNodeList =  sDataDictionaryCodeNodeMapper.queryAll(paramsMap);
        if (!codeNodeList.isEmpty()){
            for(SDataDictionaryCodeNode codeType:codeNodeList){
                ztree = new VoZtree();
                ztree.setId(codeType.getId());
                ztree.setpId(codeType.getParentId());
                ztree.setName(codeType.getName());
                ztree.setCodeType(codeType.getCodeType());
                ztree.setCode(codeType.getCode());
                //代码属性为代码类别
               // ztree.setCodeProperty("1");
                ztreeList.add(ztree);
            }
        }
        return ztreeList;
    }

    @Override
    public ResultPage pageQueryDictionNaryList(String id, int pageIndex, int pageSize) {
        ResultPage resultPage = new ResultPage();

        Page page = PageHelper.startPage(pageIndex, pageSize, true);
        JSONArray jsonArray = sDataDictionaryCodeMapper.pageQueryDictionNaryList(id);
        resultPage.setCode(0);
        resultPage.setPageIndex(pageIndex);
        resultPage.setPageSize(pageSize);
        resultPage.setAllcount(page.getTotal());
        resultPage.setResult(jsonArray);
        return resultPage;
    }

    @Override
    public int saveDataDictionary(JSONObject params) {
        //获取 code属性 0位代码类型 1为字典码  不同的类型存储不同的表
        String codeProperty = ConvertUtil.toString(params.get("codeProperty"));
        String id = ConvertUtil.toString(params.get("id"));
        String codeProperty_0 = "0";
        int result = 0;
        //为0的时候存储到sdataDictionaryCode表中
        if(codeProperty_0.equals(codeProperty)){
            //代码分类表
            SDataDictionaryCode  sDataDictionaryCode = ConvertUtil.toObject(params.toString(),SDataDictionaryCode.class);

            sDataDictionaryCode.setRegionCode("0111");
            sDataDictionaryCode.setIsAllowDelete("1");
            sDataDictionaryCode.setIsAllowEdit("1");
           // sDataDictionaryCode.setCodeProperty(codeProperty);
            sDataDictionaryCode.setIsValid("1");
            sDataDictionaryCode.setCreatorId("123");
            sDataDictionaryCode.setCreatorName("张三");
            sDataDictionaryCode.setCreateDate(new Date());
            sDataDictionaryCode.setModifyDate(new Date());
            if(StringUtil.isEmpty(id)){
                sDataDictionaryCode.setId(ConvertUtil.getUUID20());
                result = sDataDictionaryCodeMapper.insert(sDataDictionaryCode);
            }else{
                sDataDictionaryCode.setModifyId("123");
                sDataDictionaryCode.setModifyName("李四");
                result =  sDataDictionaryCodeMapper.updateByPrimaryKey(sDataDictionaryCode);
            }
        }else{
            //这个是字典表 代码表
            SDataDictionaryCodeNode sDataDictionaryCodeNode = ConvertUtil.toObject(params.toString(),SDataDictionaryCodeNode.class);

            sDataDictionaryCodeNode.setRegionCode("0000");
            sDataDictionaryCodeNode.setIsSendAble("1");
            sDataDictionaryCodeNode.setIsAllowAdd("1");
            sDataDictionaryCodeNode.setIsAllowEdit("1");
            sDataDictionaryCodeNode.setIsValid("1");
            sDataDictionaryCodeNode.setCreatorId("123");
            sDataDictionaryCodeNode.setCreatorName("张三");
            sDataDictionaryCodeNode.setCreateDate(new Date());
            sDataDictionaryCodeNode.setModifyDate(new Date());
            if(StringUtil.isEmpty(id)){
                sDataDictionaryCodeNode.setId(ConvertUtil.getUUID20());
                result = sDataDictionaryCodeNodeMapper.insert(sDataDictionaryCodeNode);
            }else{
                sDataDictionaryCodeNode.setModifyId("123");
                sDataDictionaryCodeNode.setModifyName("李四");
                result =  sDataDictionaryCodeNodeMapper.updateByPrimaryKey(sDataDictionaryCodeNode);
            }

        }
        return result;
    }

    @Override
    public int deleteDataCode(JSONObject params) {
        String codeProperty = ConvertUtil.toString(params.get("codeProperty"));
        String id = ConvertUtil.toString(params.get("id"));
        //当删除的是 字典类别的时候  需要对 字典表中该id为parentId的数据全部无效

        try {
            if("0".equals(codeProperty)){
                sDataDictionaryCodeMapper.deleteById(id);
                sDataDictionaryCodeNodeMapper.deleteByParentId(id);
            }else{
                sDataDictionaryCodeNodeMapper.deleteByParentIdOrId(id);
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


}
